import { QuickActionController } from '../controller';
import { KeyboardConst, ModeTypeConst } from '../constant';
import { DefaultModeDataProvider } from '../provider';
import { quickActionModeRegister } from '../quick-action-mode-register';

/**
 * 隐藏操作栏
 *
 * @author chitanda
 * @date 2022-07-07 15:07:45
 */
function hideQuickActionBar(): void {
  // 如果操作栏已经隐藏或鼠标悬浮在操作栏区域，则不做任何操作
  if (!window.quickAction.isHidden && !window.quickAction.isHover) {
    window.quickAction.hide();
  }
}

/**
 * 插件入口挂载后默认执行
 *
 * @author chitanda
 * @date 2022-06-28 15:06:42
 * @export
 */
export default function () {
  if (window.isQuickActionInit) {
    return;
  }
  // 快速操作栏是否已经初始化
  window.isQuickActionInit = true;
  // 挂载快捷操作栏控制器
  window.quickAction = new QuickActionController();
  // 注册默认模式
  quickActionModeRegister.addModel({
    title: '默认模式',
    type: ModeTypeConst.DEFAULT,
    keyboards: KeyboardConst.CTRL_P,
    tag: '',
    provider: new DefaultModeDataProvider(),
  });
  // 注册全局事件隐藏操作栏
  window.addEventListener('mousedown', hideQuickActionBar, { capture: true });
  window.addEventListener('blur', hideQuickActionBar, { capture: true });
  window.addEventListener('resize', hideQuickActionBar, { capture: true });
}
